﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="CreateFirewallRuleWithIpDetect.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace AzureManagement.SqlAzureCommands
{
    using System;

    using AzureManagement.AzureCommands;

    using RestClient;

    public class CreateFirewallRuleWithIpDetect : AzureManagementCommand
    {
        private readonly string serverName;

        private readonly string ruleName;

        public CreateFirewallRuleWithIpDetect(IAzureServiceContext serviceContext, string serverName, string ruleName)
            : base(serviceContext)
        {
            if (string.IsNullOrEmpty(serverName))
            {
                throw new ArgumentException("Server name must not be null or empty.");
            }

            if (string.IsNullOrEmpty(ruleName))
            {
                throw new ArgumentException("Rule name must not be null or empty.");
            }

            this.serverName = serverName;
            this.ruleName = ruleName;
        }

        protected override RequestContext GetRequestContext()
        {
            return this.CreatePostOperationRequestContext();
        }

        protected override string FormatPath(IAzureServiceContext azureServiceContext)
        {
            return string.Format(
                "{0}/servers/{1}/firewallrules/{2}", azureServiceContext.SubscriptionId, this.serverName, this.ruleName);
        }

        protected override void AddParameters(RequestContext requestContext)
        {
            requestContext.AddParameter("op", "AutoDetectClientIP");
        }
    }
}